define(function(require, exports){
	var $ = require("jquery");
	var util = require('util');
	var pubjs = require('pubjs');
	var view  = require('@base/view');
	var common = require('modules/common');
	var addQualification = require('plan/popwin/addQualification').base;
	var dspUtil = require('dspUtil');
	var utilDSP = require('modules/common');
	var CampaignType = pubjs.config("CONST/CAMPAIGN_TYPE");
	// 活动编辑基础模块
	var Base = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-campaignEdit',
				'tplFile':	'/tpl/campaign/edit.tpl',
				'steps': ['basic', 'set', 'orient', 'product'],
				'url': {
					'info': 'campaign/info',
					'add': 'campaign/add',
					'update': 'campaign/update',
					'draft': 'campaign/draft',

					'planInfo': 'plan/info'
				},
				'vModel': {
					'STEPTEXT': [
						LANG('推广组基本信息'),
						LANG('推广组设置'),
						LANG('推广组定向'),
						LANG('创意包和落地页')
					],
					'STEP': 0,
					'showSave': false,
					'showButtons': true,

					// 事件
					'vmClickNext': this.eventNext,
					'vmClickPrev': this.eventPrev,
					'vmClickSave': this.eventSave,
					'vmClickCancel': this.eventCancel,
					'vmClickSaveDraft': this.eventSaveDraft,

					'view': {

					}
					,'stepDom' : false
				},
			});

			this.$planData = null;

			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			self.uiProxy(el, '.P-campaignEditTab .step li', 'click', 'eventGoStep');

			pubjs.loading.show();

			self.buildFromTemplate({
				step:{
					data:[
						{'name':'plan','title':LANG('新建计划'),'class':'right'},
						{'name':'campaign','title':LANG('新建推广组'),'class':'having'}
					]
				}
			});
		},
		afterBuildTemplate: function(){
			pubjs.loading.hide();
		},
		onDropChange: function(ev){
			if(ev.name == 'plan'){
				var id = +ev.param.id;
				this.loadPlan(id);
				this.cast('PlanChange', ev.param);
			}
		},
		loadPlan: function(id){
			var self = this;
			var c = self.getConfig();
			if(id){
				self.showLoading();
				pubjs.mc.send(c.url.planInfo, {_id: +id}, self.onPlanData.bind(self));
			}
		},
		onPlanData: function(err, data){
			this.hideLoading();
			if (err) {
				pubjs.error(err);
				pubjs.alert(err.message);
				return false;
			}

			this.$planData = data;
			this.cast('loadPlanData', data);
		},
		load: function(id, isCopy){
			var self = this;
			self.showLoading();
			var c = self.getConfig();
			if(id)
			{
				if(!isCopy)
				{
					self.$isEdit = true;
					self.$id = +id;
				}
			}

			pubjs.mc.send(c.url.info, {_id: +id}, self.onData.bind(self), isCopy);
			return self;
		},
		onData: function(err, data, isCopy){
			this.hideLoading();
			if(err) {
				return pubjs.alert(err.message);
			}

			// 复制需要删除ID
			if (isCopy)
			{
				data.$copyId = data._id;
				data._id = null;
				this.$id = null;
				this.$isEdit = false;
			}

			this.setData(data);
		},
		setIsEdit: function(boolen){
			this.$isEdit = !!boolen;
			return this;
		},
		setData: function(data){
			var self = this;
			var c = self.getConfig();

			self.vm.set('showSave', true);
			util.each(c.steps, function(item){
				if(item && self.get(item)){
					self.get(item).setData(data);
				}
			});

			return this;
		},
		setStep :function(data){
			var self = this;
			if(data && data.stepDom){
				self.vm.$.stepDom = data.stepDom;
			}
			return self;
		},
		getData: function(){
			var self = this;
			var c = self.getConfig();

			var ret = {Type: CampaignType.RTB};

			util.each(c.steps, function(item){
				if(item && self.get(item)){
					var data = self.get(item).getData();
					ret = util.extend(ret, data);
				}
			});

			// 加入计划的广告主id
			if(this.$planData && this.$planData.AdvertiserId){
				ret.AdvertiserId = this.$planData.AdvertiserId;
			}

			return ret;
		},
		// 验证
		validate: function(data, isDraft, cb){
			var self = this;
			var c = self.getConfig();
			if(isDraft)
			{
				return cb();
			}

			if(!data)
			{
				return;
			}

			var pass = false;
			// util.each(c.steps, function(item){
			// 	if(item && self.get(item)){
			// 		pass = self.get(item).validate();
			// 		if(!pass){
			// 			return;
			// 		}
			// 	}
			// });

			var steps = c.steps;
			for(var i=0; i<steps.length; i++){
				if(self.get(steps[i])){
					pass = self.get(steps[i]).validate();
					if(!pass){
						break;
					}
				}
			}

			return pass;
		},
		eventGoStep: function(evt, elm){
			var index = $(elm).attr('data-id');
			if(index >= this.getConfig().steps.length-1){
				this.vm.set('showSave', true);
			}
			this.vm.set({'STEP': index});
			return false;
		},
		eventNext: function(){
			this.next();
		},
		eventPrev: function(){
			this.prev();
		},
		eventSave: function(){
			this.save();
		},
		eventCancel: function(){
			this.cancel();
		},
		next: function(){
			var self = this;
			var c = self.getConfig();
			var index = ++self.vm.$.STEP;

			if(index >= c.steps.length-1){
				self.vm.set('showSave', true);
			}

			self.vm.set({'STEP': index});
		},
		prev: function(){
			var self = this;
			var index = --self.vm.$.STEP;
			if(index>=0){
				this.vm.set({'STEP': index});
			}else{
				if(utilDSP.getBrandSearch()){
					pubjs.controller.navigate("serving/planEdit"+ utilDSP.getBrandSearch());
				}else{
					pubjs.controller.navigate(-1);
				}
			}
		},
		cancel: function(){
			var self = this;
			pubjs.controller.navigate(-1);
		},
		save: function(draft){
			var url;
			var self = this;
			var data = self.getData();
			var c = self.getConfig('url');

			if(self.validate(data)){
				if(self.$isEdit && self.$id){
					data._id = self.$id;
				}
				if (draft){
					url = c.draft;
				}else if(data._id){
					url = c.update;
				}else{
					url = c.add;
				}
				self.showLoading();
				pubjs.mc.send(url, data, self.afterSave.bind(self));
			}
		},
		afterSave: function(err, data){
			this.hideLoading();
			if (err){
				pubjs.alert(err.message);
				return false;
			}

			this.$data = data;

			if (common.checkOpener() && window.opener && window.opener.pubjs)
			{
				window.opener.pubjs.core.cast('saveCampaignSuccess', {"id": data._id});
			}

			pubjs.controller.navigate(-1);
		},
		saveDraft: function(){
			var self = this;
			pubjs.confirm(LANG('确定保存为草稿吗？'), function(isOk){
				if(isOk){
					self.save(true);
				}
			});
		},
		reset: function(){
			var self = this;
			self.vm.reset();
			var c = self.getConfig();

			self.$id = null;
			self.$planData = null;
			c.stepDom = false;

			util.each(c.steps, function(item){
				if(item && self.get(item)){
					self.get(item).reset();
				}
			});

			return this;
		}
	});

	exports.base = Base;

});